﻿using KZ.Express.ENT;
using KZ.Express.Resource;
using KZ.Utilities;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace KZ.Express.Task
{
    public class LoginAccountTask : TaskBase
    {
        private new UserENT _entity;

        public LoginAccountTask()
            : base()
        {
            base._entity = _entity = new UserENT();
            _entitytype = _entity.GetType();
        }

        public void Save(List<DataRow> rows)
        {
            base._dalBase.BeginTransaction();

            try
            {
                foreach (DataRow row in rows)
                {
                    string fullName = Utility.StringParse(row["FullName"]);
                    string username = Utility.StringParse(row["Username"]);
                    string password = Utility.StringParse(row["Password"]);
                    if (!String.IsNullOrEmpty(fullName) && !String.IsNullOrEmpty(username) && !String.IsNullOrEmpty(password))
                    {
                        UserENT user = new UserENT();
                        user.UserID = Utility.IntParse(row["UserID"]);
                        user.FullName = fullName;
                        user.Username = username;
                        user.Password = password;
                        user.RoleID = Utility.IntNParse(row["RoleID"]);
                        user.CreatedBy = UserENT.USERID;
                        user.UpdatedBy = UserENT.USERID;

                        if (user.UserID == 0) base._dalBase.Insert(user);
                        else base._dalBase.Update(user);
                    }
                }

                base._dalBase.CommitTransaction();
            }
            catch (DataAccessException ex)
            {
                base._dalBase.RollbackTransaction();
                throw new BusinessLogicException(ex.Message);
            }
            catch
            {
                base._dalBase.RollbackTransaction();
                throw new BusinessLogicException(Message.BLM_ERR_SAVE.ToString());
            }
        }
    }
}
